//
//  StudentDetailsViewController.m
//  Civacrm
//
//  Created by mubei on 17/2/9.
//  Copyright © 2017年 王桂敏. All rights reserved.
//

#import "StudentDetailsViewController.h"
#import "LXMPieView.h"
#import "ZFJSegmentedControl.h"
#import "SchoolStudentTableViewCell.h"
#import "GetStudentInfoModel.h"
#import "WithoutDataTableViewCell.h"
#import "BottomView.h"
@interface StudentDetailsViewController ()
@property (nonatomic, strong) LXMPieView *PieCharViewStudent;
@property (nonatomic, strong) UILabel *firstStudentLable;//学生数据
@property (nonatomic, strong) UILabel *firstMiddelStudentLable;
@property (nonatomic, strong) UILabel *studentLable;
@property (nonatomic, strong) ZFJSegmentedControl *zvc7;
@property (nonatomic, strong) NSMutableArray *studentArray;
@property (nonatomic, strong) NSArray *typeArray;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, copy) NSString *oldType;

@end

@implementation StudentDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageNo = 1;
    self.pageSize = 10;
    self.studentArray = [NSMutableArray array];
    self.typeArray = @[@"-1",@"1",@"0",@"2"];
    self.type = @"-1";
    [self initNavigation];
    [self initView];
    [self initlowView];
    [self initTableView];
    
    [self initNetWork:@"-1"];
    
    
}
- (void)initNavigation{
    UIView *topView = [[UIView alloc] init];
    topView.frame = CGRectMake(0,0, getW(self.navigationController.navigationBar), getH(self.navigationController.navigationBar) );
    UILabel * firstLable = [UILabel labelWithTitle:@"学生激活情况详情" frame:CGRectMake(0,0, 0, 0 ) font:font(32) textColor:WhiteColor bgColor:ClearColor];
    [firstLable sizeToFit];
    firstLable.centerX = topView.width/2 - w(100);
    UILabel *secondLable  = [UILabel labelWithTitle:self.orgName frame:CGRectMake(0, getMaxY(firstLable), 0, 0) font:font(24) textColor:WhiteColor bgColor:ClearColor];
    [secondLable sizeToFit];
    secondLable.centerX = firstLable.centerX;
    [topView addSubview:firstLable];
    [topView addSubview:secondLable];
    
    self.navigationItem.titleView =  topView;

}
- (void)initView{
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, h(343))];
    topView.backgroundColor = WhiteColor;
    [self.view addSubview:topView];
    NSMutableArray *modelArray = [NSMutableArray array];
    CGFloat studentFloat = 0;
    CGFloat studentVip = 0;
    if (self.schoolPageInfoModel.studentCounts != 0) {
        studentVip = (CGFloat)self.schoolPageInfoModel.vipCounts/self.schoolPageInfoModel.studentCounts;
        studentFloat = (CGFloat)(self.schoolPageInfoModel.studentActivates - self.schoolPageInfoModel.vipCounts)/self.schoolPageInfoModel.studentCounts;
    }
    
    NSArray *valueArray = @[ @(1- studentFloat - studentVip),@(studentFloat), @(studentVip)];

    NSArray *colorArray = @[ColorWithHexString(@"f6e5bd"),
                            ColorWithHexString(@"edb463"),
                            ColorWithHexString(@"f6e04b")];
    for (int i = 0 ; i <valueArray.count ; i++) {
        LXMPieModel *model = [[LXMPieModel alloc] initWithColor:colorArray[i] value:[valueArray[i] floatValue] text:nil];
        [modelArray addObject:model];
    }
    
    self.PieCharViewStudent = [[LXMPieView alloc] initWithFrame:CGRectMake(w(70),  h(74),w(180), h(180))values:modelArray];
    //self.PieCharViewStudent.delegate = self;
    self.PieCharViewStudent.centerX = self.view.width/2;
    [topView addSubview:self.PieCharViewStudent];
    
    if (self.schoolPageInfoModel.vipCounts != 0) {
        UILabel *litleCircleLable = [UILabel labelWithTitle:nil frame:CGRectMake(self.view.width/2 - w(8), getMinY(self.PieCharViewStudent) - h(8), w(8), h(8)) font:nil textColor:nil bgColor:ColorWithHexString(@"f6e04b")];
        litleCircleLable.layer.masksToBounds = YES;
        litleCircleLable.layer.cornerRadius = h(4);
        [topView addSubview:litleCircleLable];
        
        
        
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(getMinX(litleCircleLable)+w(8/2), getMinY(litleCircleLable)-h(20) + h(8/2), w(20), h(20))];
        [topView addSubview:imageView];
        
        UIGraphicsBeginImageContext(imageView.frame.size);
        [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0);
        CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 246.0/255.0, 224.0/255, 75.0/255,1.0);
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 0, h(20));
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),w(20), 0);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        imageView.image=UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        UILabel *lowLable = [UILabel labelWithTitle:nil frame:CGRectMake(getMaxX(imageView), getMinY(imageView), 0, h(2)) font:nil textColor:nil bgColor:ColorWithHexString(@"f6e04b")];
        self.firstStudentLable = [UILabel labelWithTitle:[NSString stringWithFormat:@"%ld人已付费",self.schoolPageInfoModel.vipCounts] frame:CGRectMake(getMaxX(imageView), getMinY(imageView), 0, 0) font:font(24) textColor:ColorWithHexString(@"f6e04b") bgColor:nil];
        self.firstStudentLable.font = [UIFont fontWithName:@"Helvetica-Bold" size:((24 / 2) / kScreenHeight6) * kScreenHeight];
        
        [self.firstStudentLable sizeToFit];
        self.firstStudentLable.y  = self.firstStudentLable.y - self.firstStudentLable.height;
        [topView addSubview:self.firstStudentLable];
        
        lowLable.width = self.firstStudentLable.width;
        [topView addSubview:lowLable];
    }
    
    UILabel *bigCircleLable = [UILabel labelWithTitle:nil frame:CGRectMake(w(70)+w(32), getMinY(self.PieCharViewStudent) + h(32), (w(90)-w(32))*2, (w(90)-w(32))*2) font:nil textColor:nil bgColor:WhiteColor];
    bigCircleLable.layer.masksToBounds = YES;
    bigCircleLable.layer.cornerRadius = h(90)-h(32);
    bigCircleLable.center = self.PieCharViewStudent.center;
    [topView addSubview:bigCircleLable];
    
    self.firstMiddelStudentLable = [[UILabel alloc] init];
    self.firstMiddelStudentLable.frame = CGRectMake(0, 0, 0, 0);
    
    NSString *string = [NSString stringWithFormat: @"%ld/%ld",self.schoolPageInfoModel.studentActivates,self.schoolPageInfoModel.studentCounts];
    NSString *studentActivates = [NSString stringWithFormat:@"%ld",self.schoolPageInfoModel.studentActivates];
    NSMutableAttributedString *mutableAttriteStr = [[NSMutableAttributedString alloc] init];
    NSAttributedString *attributeStr1 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(0, studentActivates.length)] attributes:@{NSFontAttributeName :[UIFont fontWithName:@"Helvetica-Bold" size:((32 / 2) / kScreenHeight6) * kScreenHeight],NSForegroundColorAttributeName : ColorWithHexString(@"edb463"),NSBackgroundColorAttributeName :WhiteColor}];
    NSAttributedString *attributeStr4 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(studentActivates.length, string.length-studentActivates.length)] attributes:@{NSFontAttributeName :font(20),NSForegroundColorAttributeName : ColorWithHexString(@"edb463"),NSBackgroundColorAttributeName : WhiteColor}];
    [mutableAttriteStr appendAttributedString:attributeStr1];
    [mutableAttriteStr appendAttributedString:attributeStr4];
    
    self.firstMiddelStudentLable.attributedText = mutableAttriteStr;
    [self.firstMiddelStudentLable sizeToFit];
    self.firstMiddelStudentLable.center = bigCircleLable.center;
    [topView addSubview:self.firstMiddelStudentLable];
    
    self.studentLable = [UILabel labelWithTitle:@"学生激活情况" frame:CGRectMake(getMinX(self.PieCharViewStudent), getMaxY(self.PieCharViewStudent) + h(21), 0, 0) font:font(24) textColor:ColorWithHexString(@"edb463") bgColor:nil];
    [self.studentLable sizeToFit];
    self.studentLable.centerX = self.PieCharViewStudent.centerX;
    [topView addSubview:self.studentLable];
    


}
- (void)initlowView{
    NSArray *titleArr = @[@"全部",@"已激活",@"未激活",@"已付费"];
    self.zvc7 = [[ZFJSegmentedControl alloc]initwithTitleArr:titleArr iconArr:nil SCType:SCType_Underline];
    self.zvc7.frame = CGRectMake(0, getMaxY(self.studentLable) + h(43),self.view.width, h(86));
    self.zvc7.centerX = self.view.width/2;
    self.zvc7.backgroundColor = [UIColor whiteColor];
    self.zvc7.titleColor = ColorWithHexString(@"98c1d4");
    self.zvc7.selectBtnSpace = 0;//设置按钮间的间距
//    zvc7.selectBtnWID = ;//设置按钮的宽度 不设就是均分
    self.zvc7.titleFont = font(24);//按钮的常态样式
    self.zvc7.selectTitleColor = GlobalBlue;
    self.zvc7.SCType_Underline_HEI = 2;//设置底部横线的高度
    __weak StudentDetailsViewController *weakSelf = self;
    
    self.zvc7.selectType = ^(NSInteger selectIndex,NSString *selectIndexTitle){
        NSLog(@"selectIndexTitle == %@",selectIndexTitle);
        weakSelf.type = weakSelf.typeArray[selectIndex];
        [weakSelf initNetWork:weakSelf.type];
        [weakSelf.tableView setContentOffset:CGPointMake(0,0) animated:NO];

    };
    [self.view addSubview:self.zvc7];

}
- (void)initTableView{
    self.tableView.frame = CGRectMake(0, getMaxY(self.zvc7), self.view.width, self.view.height - getMaxY(self.zvc7) - h(80) - 64);
    [self.tableView registerClass:[SchoolStudentTableViewCell  class] forCellReuseIdentifier:@"Cell"];
    [self.tableView registerClass:[WithoutDataTableViewCell  class] forCellReuseIdentifier:@"item"];
    self.tableView.mj_footer = self.footer;
    self.tableView.mj_header = self.header;

    [self.footer  setTitle:@"已经到底啦" forState:MJRefreshStateNoMoreData];
    self.footer .stateLabel.font = font(24);
    self.footer.stateLabel.textColor = ColorWithHexString(@"cfdfe8");
    [self.view addSubview:self.tableView];
    
    BottomView * bottomView = [BottomView initBottomViewWithFrame:CGRectMake(0, kScreenHeight - h(80) - 64, kScreenWidth, h(80)) andActivateNumString:self.activeString andPayForNumString:self.payforString];
    [self.view addSubview:bottomView];

}
#pragma mark tabelView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.studentArray.count>0) {
        tableView.scrollEnabled = YES;

        SchoolStudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.getStudentInfoModel = self.studentArray[indexPath.row];
        return cell;
    }else{
        tableView.scrollEnabled = NO;
        WithoutDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"item"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
        
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.studentArray.count>0) {
        return self.studentArray.count;

    }else{
        return 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.studentArray.count>0) {
        return h(114);
        
    }else{
        return self.view.height - getMaxY(self.zvc7);
    }
}

#pragma mark 下拉刷新和上拉加载

- (void)handleHeaderRefreshingAction{
    
    self.pageNo = 1;
    [self initNetWork:self.type];
}

- (void)handleFooterRefreshingAction{
    
    self.pageNo++;

    [self initNetWork:self.type];
    
}
- (void)initNetWork:(NSString *)type{
    //
    if (![self.oldType isEqualToString:type]) {
        self.pageNo = 1;
    }
    self.oldType = type;
    self.params[@"type"] = type;
    self.params[@"pageNo"] = [NSString stringWithFormat:@"%@",@(self.pageNo)];
    self.params[@"pageSize"] = [NSString stringWithFormat:@"%@",@(self.pageSize)];
    [self showIndeterminate];
    [self.manager postWithURL:NET_GETSTUDENTINFO param:self.params delegate:self isEncrypt:NO type:NetWorkingType0];
    

}
- (void)networkingHandleSuccess:(NetworkingHandle *)handle parameters:(NSDictionary *)parameters responseObject:(id)responseObject type:(NetWorkingType)type{
    [self hideHud];
    switch (type) {
        case NetWorkingType0:
        {
            [self endRefresh];
            
            if (self.pageNo == 1) {
                [self.studentArray removeAllObjects];
            }
            
            NSArray *array  = [responseObject objectForKey:@"items"];
            for (NSDictionary *dic in array) {
                GetStudentInfoModel *model = [[GetStudentInfoModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.studentArray addObject:model];
            }
            if (array.count < self.pageSize) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
                
                
            }
            [self.tableView reloadData];
        }
            break;
            
        default:
            break;
    }
    
    
    
}

- (void)networkingHandlefail:(NetworkingHandle *)handle type:(NetWorkingType)type{
    [self hideHud];
    [self endRefresh];

}

- (void)networkingHandleWarning:(NetworkingHandle *)handle responseObject:(id)responseObject type:(NetWorkingType)type{
    [self hideHud];
    [self endRefresh];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
